package com.example.a58;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

import dao.BookDao;
import entity.BookInfo;

public class RoomWriteActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name;
    private EditText et_press;
    private EditText et_price;
    private EditText et_author;
    private Button btn_search;
    private Button btn_fix;
    private Button btn_del;
    private Button btn_add;
    private BookDao bookDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_room_write);
        btn_add = findViewById(R.id.btn_add);
        btn_del = findViewById(R.id.btn_del);
        btn_fix = findViewById(R.id.btn_fix);
        btn_search = findViewById(R.id.btn_search);

        et_name = findViewById(R.id.et_name);
        et_author = findViewById(R.id.et_author);
        et_press = findViewById(R.id.et_press);
        et_name = findViewById(R.id.et_name);
        et_press = findViewById(R.id.et_price);

        btn_add.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_search.setOnClickListener(this);
        btn_fix.setOnClickListener(this);

        bookDao = Myapplication.getInstance().getBookDB().bookDao();
    }

    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String author = et_author.getText().toString();
        String press = et_press.getText().toString();
        String price = et_price.getText().toString();

        if(v.getId() == R.id.btn_add){

            BookInfo b1 = new BookInfo();
            b1.setAuthor(author);
            b1.setPress(press);
            b1.setPrice(Integer.valueOf(price));
            b1.setName(name);

            bookDao.insert(b1);
            Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show();
        }else if (R.id.btn_del == v.getId()){
            BookInfo b2 = new BookInfo();
            b2.setId(1);
            bookDao.delete(b2);
        }else if (R.id.btn_fix == v.getId()){
            BookInfo b1 = new BookInfo();
            BookInfo b2 = bookDao.queryByName(name);
            b1.setId(b2.getId());
            b1.setAuthor(author);
            b1.setPress(press);
            b1.setPrice(Integer.valueOf(price));
            b1.setName(name);
            bookDao.uodate(b1);


        }else if (R.id.btn_search == v.getId()){
            List<BookInfo> list = bookDao.queryAll();
            for (BookInfo b:list){
                Log.e("TAG",""+b );
            }
        }
    }
}